﻿using ConsoleApp2.Themeup;
using FourOperationsDemo.BaseDate;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace ConsoleApp2.Base
{
    public class OneTheme : IBasicTheme
    {
        readonly Random random = new Random();

        public string Add { get; set; }
        public int DM { get; set; }
        public string Sub { get; private set; }
        public readonly string[] vsArray1 = new string[10];
        public readonly string[] vsArray2 = new string[10];
        /// <summary>
        /// 一年级出题范围：20以内加减
        /// </summary>
        public OneTheme()
        {
            Randomnum();
        }
        public void Randomnum()
        {
            BasicDataA = random.Next(0, 20);
            BasicDataB = random.Next(0, 20);
        }


        public string AddTheme()
        {
            try
            {
                if (BasicDataA > 20 && BasicDataB > 20)
                {
                    throw new Exception("A>20 并且 B>20");
                }
                else
                {
                    Add = BasicDataA + "+" + BasicDataB;
                    DM = BasicDataA + BasicDataB;
                    return Add;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return Add = null;

        }
        public string SubTheme()
        {
            try
            {
                if (BasicDataA > 20 && BasicDataB > 20)
                {
                    throw new Exception("A>20 并且 B>20");
                }
                else
                {
                    if (BasicDataA > BasicDataB)
                    {
                        Sub = BasicDataA + "-" + BasicDataB;
                    }
                    else
                    {
                        BasicDataA += BasicDataB;
                        BasicDataB = BasicDataA - BasicDataB;
                        BasicDataA -= BasicDataB;
                        Sub = BasicDataA + "-" + BasicDataB;
                    }
                    DM = BasicDataA + BasicDataB;
                    return Sub;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return Sub = null;





        }


        /// <summary>
        ///      ///调用此方法一次出10题加法题目
        /// </summary>
        public override string AddThemeCount()
        {
            string[] Array1 = new string[10];

            for (int i = 0; i < Array1.Length; i++)
            {
                var oneTheme = new OneTheme();
                oneTheme.AddTheme();
                Array1[i] = oneTheme.Add;
                vsArray1[i] = oneTheme.Add;

            }
            //for (int i = 0; i < Array1.Length; i++)
            //{
            //    Console.WriteLine(Array1[i]);
            //}

            return Array1[9];

        }
        /// <summary>
        ///  //调用此方法一次出10题减法题目
        /// </summary>
        public override string SubThemeCount()
        {
            string[] Array2 = new string[10];

            for (int i = 0; i < Array2.Length; i++)
            {
                var oneTheme = new OneTheme();
                oneTheme.SubTheme();
                Array2[i] = oneTheme.Sub;
                vsArray2[i] = oneTheme.Sub;
            }
            //for (int i = 0; i < Array1.Length; i++)
            //{
            //    Console.WriteLine(Array1[i]);
            //}
            return Array2[9];
        }

        //创建新文件
        //第一个是路径，
        //第二个是确定当文件存在时，是覆盖掉还是将数据追加到文件中，true是将数据追加到已存在文件中，false是覆盖已存在的文件。
        //第三个参数是要使用的字符编码，一般是使用默认的。
        //StreamWriter writer = new StreamWriter(result, false, Encoding.Default);
        //writer.Flush();



        /// <summary>
        /// 存在无法输出bug ，已经修复可以抓取数据  vsArray1[i]
        /// </summary>
        public void AddOutPutTxt()
        {
            string result = @"E:\打印文件.txt";//保存文件路径
            this.AddThemeCount();
            //定义写文件流,打开文件或者创建文件
            FileStream fs = new FileStream(result, FileMode.OpenOrCreate);
            //写入的内容
            StreamWriter wr = new StreamWriter(fs);
            for (int i = 0; i < vsArray1.Length; i++)
            {
                wr.WriteLine(vsArray1[i]);
            }
            wr.Flush();
            wr.Close();
            //关闭文件流
            fs.Close();
        }

        public void SubOutPutTxt()
        {
            string result = @"D:\打印文件1.txt";//保存文件路径
            OneTheme oneTheme = new OneTheme();
            oneTheme.SubThemeCount();
            //定义写文件流,打开文件或者创建文件
            FileStream fs = new FileStream(result, FileMode.OpenOrCreate);
            //写入的内容
            StreamWriter wr = new StreamWriter(fs);
            for (int i = 0; i < vsArray2.Length; i++)
            {
                wr.WriteLine(oneTheme.vsArray2[i]);
            }
            wr.Flush();
            wr.Close();
            //关闭文件流
            fs.Close();
        }


        public void ReaderTxt()
        {
            using (StreamReader sr = new StreamReader(@"D:\打印文件.txt"))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
            using (StreamReader sr = new StreamReader(@"D:\打印文件1.txt"))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }


        }

        //public void WriteXMLAdd(string fileName)
        //{
        //    //OneTheme oneTheme = new OneTheme();
        //    XmlWriterSettings settings = new XmlWriterSettings();
        //    settings.Indent = true;
        //    using (XmlWriter writer = XmlWriter.Create("..\\..\\..\\" + fileName, settings))
        //    {
        //        writer.WriteStartElement("生成题目");//开始写入标记               
        //        for (int i = 0; i < vsArray1.Length; i++)
        //        {
        //            //oneTheme.AddThemeCount();
        //            writer.WriteElementString("题目", vsArray1[i]);

        //        }
        //        writer.WriteEndElement();
        //    }
        //    Console.WriteLine("写入成功");
        //}
        public void WriteXMLAdd(string fileName)
        {
            //var oneTheme = new OneTheme();
            XmlWriterSettings settings = new XmlWriterSettings();//XML文件设置
            settings.Indent = true;//换行
            using (XmlWriter writer = XmlWriter.Create("..\\..\\..\\" + fileName, settings))
            {
                writer.WriteStartElement("生成题目");//节点头

                for (int i = 0; i < vsArray1.Length; i++)
                {
                    writer.WriteElementString("题目", vsArray1[i]);
                }
                writer.WriteEndElement();
            }
            Console.WriteLine("写入成功");
        }

        //public void WriteXMLDoc(string fileName)
        //{
        //    XmlDocument document = new XmlDocument();

        //}




    }
}
